from PyQt5.QtCore import QDateTime, pyqtSignal
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
import sys
import cv2
import ctypes

from PyQt5.uic.properties import QtWidgets, QtCore

from player_2channel import Ui_MainWindow


# class myVideoSurface(QAbstractVideoSurface):
#
#     FinishGrab = pyqtSignal()  # 截图完成信号
#
#     def __init__(self, parent=None):
#         super(QAbstractVideoSurface, self).__init__(parent)
#
#     def supportedPixelFormats(self, type=None):
#         support_format = [
#             QVideoFrame.Format_ARGB32,
#             QVideoFrame.Format_ARGB32_Premultiplied,
#             QVideoFrame.Format_ARGB8565_Premultiplied,
#             QVideoFrame.Format_AYUV444,
#             QVideoFrame.Format_AYUV444_Premultiplied,
#             QVideoFrame.Format_BGR24,
#             QVideoFrame.Format_BGR32,
#             QVideoFrame.Format_BGR555,
#             QVideoFrame.Format_BGR565,
#             QVideoFrame.Format_BGRA32,
#             QVideoFrame.Format_BGRA32_Premultiplied,
#             QVideoFrame.Format_BGRA5658_Premultiplied,
#             QVideoFrame.Format_CameraRaw,
#             QVideoFrame.Format_IMC1,
#             QVideoFrame.Format_IMC2,
#             QVideoFrame.Format_IMC3,
#             QVideoFrame.Format_IMC4,
#             QVideoFrame.Format_Jpeg,
#             QVideoFrame.Format_NV12,
#             QVideoFrame.Format_NV21,
#             QVideoFrame.Format_RGB24,
#             QVideoFrame.Format_RGB32,
#             QVideoFrame.Format_RGB555,
#             QVideoFrame.Format_RGB565,
#             QVideoFrame.Format_User,
#             QVideoFrame.Format_UYVY,
#             QVideoFrame.Format_Y16,
#             QVideoFrame.Format_Y8 ,
#             QVideoFrame.Format_YUV420P,
#             QVideoFrame.Format_YUV444,
#             QVideoFrame.Format_YUYV,
#             QVideoFrame.Format_YV12,
#         ]
#         return support_format
#
#     def present(self, widget: 'QVideoWidget'):
#
#
#         #widget.map(QAbstractVideoBuffer.ReadOnly)
#         img = QImage(widget.bits(), widget.width(), widget.height(),
#                         QVideoFrame.imageFormatFromPixelFormat(widget.pixelFormat()))
#         grab_jpg = './screen-shot/'+QDateTime.currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz")+'.jpg'
#         save_state = img.save(grab_jpg)
#         print("截图状态："+str(save_state))
#         #widget.unmap()
#         self.FinishGrab.emit()



class Mywindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        ##一号播放器操作
        self.player1 = QMediaPlayer()
        self.player1.setVideoOutput(self.widget_1)
        self.btn_root_1.clicked.connect(self.openVideoFile1)
        self.btn_play_1.clicked.connect(self.playVideo1)
        self.btn_pause_1.clicked.connect(self.pauseVideo1)
        #self.btn_scnst_1.clicked.connect(self.present(widget=self.widget_1))

        ##二号播放器操作
        self.player2 = QMediaPlayer()
        self.player2.setVideoOutput(self.widget_2)
        self.btn_root_2.clicked.connect(self.openVideoFile2)
        self.btn_play_2.clicked.connect(self.playVideo2)
        self.btn_pause_2.clicked.connect(self.pauseVideo2)



    def openVideoFile1(self):
        video_1_root = QMediaContent(QFileDialog.getOpenFileUrl()[0])
        self.player1.setMedia(video_1_root)  # 选取视频文件

    def openVideoFile2(self):
        self.player2.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))  # 选取视频文件

    def playVideo1(self):
        self.player1.play()

    def playVideo2(self):
        self.player2.play()

    def pauseVideo1(self):
        self.player1.pause()

    def pauseVideo2(self):
        self.player2.pause()




if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = Mywindow()
    myWindow.show()
    sys.exit(app.exec_())